<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Denzel - canvas像素点获取 —— 拾色器、放大器</title>
    <meta name="description" content="前端笔记">
    <meta name="keywords" content="web前端,HTML5,CSS3,Javascript,SVG,canvas">
  <meta name="anthor" content="Denzel">
  <meta name="theme-color" content="#3eaf7c">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
  <meta name="msapplication-TileColor" content="#000000">
  <link rel="icon" href="/blog/vuepress/logo.png">
  <link rel="manifest" href="/blog/vuepress/manifest.json">
  <link rel="apple-touch-icon" href="/blog/vuepress/icons/apple-touch-icon-152x152.png">
  <link rel="mask-icon" href="/blog/vuepress/icons/safari-pinned-tab.svg" color="#3eaf7c">
    
    <link rel="preload" href="/blog/vuepress/assets/css/0.styles.d4f29607.css" as="style"><link rel="preload" href="/blog/vuepress/assets/js/app.15770e34.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/43.8f0b2d6c.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/85.44e980f9.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/84.1ffe6969.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/87.cd49f324.js" as="script"><link rel="prefetch" href="/blog/vuepress/assets/js/46.59c0ece7.js"><link rel="prefetch" href="/blog/vuepress/assets/js/1.05e4270f.js"><link rel="prefetch" href="/blog/vuepress/assets/js/2.ace739f7.js"><link rel="prefetch" href="/blog/vuepress/assets/js/3.636e5a79.js"><link rel="prefetch" href="/blog/vuepress/assets/js/4.7a55aa4b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/5.b56b0573.js"><link rel="prefetch" href="/blog/vuepress/assets/js/6.e9a3fbc8.js"><link rel="prefetch" href="/blog/vuepress/assets/js/7.aa46c47d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/8.a9288ac6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/9.b9f8dec6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/10.3e2ea470.js"><link rel="prefetch" href="/blog/vuepress/assets/js/11.4d115f98.js"><link rel="prefetch" href="/blog/vuepress/assets/js/12.c97cf4d0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/13.1e0cf594.js"><link rel="prefetch" href="/blog/vuepress/assets/js/14.bab9026a.js"><link rel="prefetch" href="/blog/vuepress/assets/js/15.1a24cffe.js"><link rel="prefetch" href="/blog/vuepress/assets/js/16.e9995850.js"><link rel="prefetch" href="/blog/vuepress/assets/js/17.86a1f7d2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/18.c32400c4.js"><link rel="prefetch" href="/blog/vuepress/assets/js/19.9f99887c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/20.82f0c2f9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/21.7f5c223d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/22.acf9934c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/23.20d8e817.js"><link rel="prefetch" href="/blog/vuepress/assets/js/24.acf27379.js"><link rel="prefetch" href="/blog/vuepress/assets/js/25.20165510.js"><link rel="prefetch" href="/blog/vuepress/assets/js/26.83a2ddc3.js"><link rel="prefetch" href="/blog/vuepress/assets/js/27.33276a04.js"><link rel="prefetch" href="/blog/vuepress/assets/js/28.e320efab.js"><link rel="prefetch" href="/blog/vuepress/assets/js/29.3fd92c30.js"><link rel="prefetch" href="/blog/vuepress/assets/js/30.2bc84d43.js"><link rel="prefetch" href="/blog/vuepress/assets/js/31.d70ed500.js"><link rel="prefetch" href="/blog/vuepress/assets/js/32.e7a0f8e1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/33.c48ee70b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/34.8ad03d1b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/35.5bdb3994.js"><link rel="prefetch" href="/blog/vuepress/assets/js/36.a25f926e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/37.7515cfbb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/38.99347371.js"><link rel="prefetch" href="/blog/vuepress/assets/js/39.3481fc62.js"><link rel="prefetch" href="/blog/vuepress/assets/js/40.bc709734.js"><link rel="prefetch" href="/blog/vuepress/assets/js/41.e1e0a3bf.js"><link rel="prefetch" href="/blog/vuepress/assets/js/42.762b61b0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/44.2a0a4ee0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/45.baf7ed60.js"><link rel="prefetch" href="/blog/vuepress/assets/js/47.11a8720a.js"><link rel="prefetch" href="/blog/vuepress/assets/js/48.e0bf8b54.js"><link rel="prefetch" href="/blog/vuepress/assets/js/49.c96c4011.js"><link rel="prefetch" href="/blog/vuepress/assets/js/50.31189263.js"><link rel="prefetch" href="/blog/vuepress/assets/js/51.766a8294.js"><link rel="prefetch" href="/blog/vuepress/assets/js/52.26b971fd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/53.0cfbd708.js"><link rel="prefetch" href="/blog/vuepress/assets/js/54.bdc15c87.js"><link rel="prefetch" href="/blog/vuepress/assets/js/55.fea285a9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/56.e030a6b2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/57.bd168a1e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/58.636557fa.js"><link rel="prefetch" href="/blog/vuepress/assets/js/59.5d1d0ce6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/60.0b779f92.js"><link rel="prefetch" href="/blog/vuepress/assets/js/61.4ec3f2eb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/62.8f70f114.js"><link rel="prefetch" href="/blog/vuepress/assets/js/63.9ac2b61d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/64.c028fad5.js"><link rel="prefetch" href="/blog/vuepress/assets/js/65.923a8d15.js"><link rel="prefetch" href="/blog/vuepress/assets/js/66.73db652e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/67.1a91446d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/68.e5a12a42.js"><link rel="prefetch" href="/blog/vuepress/assets/js/69.6ddcb882.js"><link rel="prefetch" href="/blog/vuepress/assets/js/70.ceb70d85.js"><link rel="prefetch" href="/blog/vuepress/assets/js/71.a804ef40.js"><link rel="prefetch" href="/blog/vuepress/assets/js/72.f1c4098c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/73.2f28b842.js"><link rel="prefetch" href="/blog/vuepress/assets/js/74.571c2ab1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/75.f365c2a1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/76.0916a3cd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/77.469aed17.js"><link rel="prefetch" href="/blog/vuepress/assets/js/78.02cdd59d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/79.44d7f95c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/80.4ca072f1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/81.d9239a49.js"><link rel="prefetch" href="/blog/vuepress/assets/js/82.b09096b2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/83.f4bc8a9b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/86.c8579ac0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/88.3851b6ff.js"><link rel="prefetch" href="/blog/vuepress/assets/js/89.b05297be.js"><link rel="prefetch" href="/blog/vuepress/assets/js/90.c1e3263e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/91.b493a1bb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/92.80ab06fd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/93.c6ca00b3.js"><link rel="prefetch" href="/blog/vuepress/assets/js/94.e993d310.js"><link rel="prefetch" href="/blog/vuepress/assets/js/95.8e19a7ed.js">
    <link rel="stylesheet" href="/blog/vuepress/assets/css/0.styles.d4f29607.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div><a href="/blog/vuepress/" class="home-link router-link-active"><img src="/blog/vuepress/hero.png" class="logo"><span class="site-name can-hide">
      Denzel
    </span></a><div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""><!----></div><nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/vuepress/js/" class="nav-link">JS</a></div><div class="nav-item"><a href="/blog/vuepress/css/" class="nav-link">CSS</a></div><div class="nav-item"><a href="/blog/vuepress/html/" class="nav-link router-link-active">HTML</a></div><div class="nav-item"><a href="/blog/vuepress/else/" class="nav-link">其他</a></div><div class="nav-item"><a href="/blog/vuepress/study/" class="nav-link">study</a></div><a href="https://github.com/xiaotianxia" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header><div class="sidebar-mask"></div><div class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/blog/vuepress/js/" class="nav-link">JS</a></div><div class="nav-item"><a href="/blog/vuepress/css/" class="nav-link">CSS</a></div><div class="nav-item"><a href="/blog/vuepress/html/" class="nav-link router-link-active">HTML</a></div><div class="nav-item"><a href="/blog/vuepress/else/" class="nav-link">其他</a></div><div class="nav-item"><a href="/blog/vuepress/study/" class="nav-link">study</a></div><a href="https://github.com/xiaotianxia" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav><ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>HTML</span><!----></p><ul class="sidebar-group-items"><li><a href="/blog/vuepress/html/" class="sidebar-link">目录</a></li><li><a href="/blog/vuepress/html/web_components.html" class="sidebar-link">浅尝 Web Components </a></li><li><a href="/blog/vuepress/html/canvas_video_green_screen_cutout.html" class="sidebar-link">canvas像素点操作 —— 视频绿幕抠图</a></li><li><a href="/blog/vuepress/html/canvas_pixel_pick.html" class="active sidebar-link">canvas像素点获取 —— 拾色器、放大器</a></li><li><a href="/blog/vuepress/html/dialog_element.html" class="sidebar-link">HTML5.2新标签 —— dialog </a></li></ul></div></li></ul></div><div class="page"><div class="content"><h1 id="canvas像素点获取-——-拾色器、放大器"><a href="#canvas像素点获取-——-拾色器、放大器" aria-hidden="true" class="header-anchor">#</a> canvas像素点获取 —— 拾色器、放大器</h1><p><a href="https://denzel.netlify.com/html/canvas_pixel_pick.html" target="_blank" rel="noopener noreferrer">原文地址</a></p><h2 id="前言"><a href="#前言" aria-hidden="true" class="header-anchor">#</a> 前言</h2><p>最近在学习canvas，然后照葫芦画瓢简单实现了几个小demo，跟大家一块学习一下。</p><h2 id="主要内容"><a href="#主要内容" aria-hidden="true" class="header-anchor">#</a> 主要内容</h2><ul><li><a href="#%E4%B8%A4%E4%B8%AA%E6%96%B9%E6%B3%95%EF%BC%9Adrawimage%E3%80%81getimagedata">两个方法：drawImage、getImageData</a></li><li><a href="#%E5%89%8D%E7%AB%AF%E5%9B%BE%E7%89%87%E9%A2%84%E8%A7%88%E3%80%81%E8%B7%A8%E5%9F%9F%E5%9B%BE%E7%89%87%E9%97%AE%E9%A2%98">前端图片预览、跨域图片问题</a></li></ul><h2 id="两个方法：drawimage、getimagedata"><a href="#两个方法：drawimage、getimagedata" aria-hidden="true" class="header-anchor">#</a> 两个方法：drawImage、getImageData</h2><h3 id="drawimage"><a href="#drawimage" aria-hidden="true" class="header-anchor">#</a> drawImage</h3><p>用法：</p><pre class="language-js"><code>context<span class="token punctuation">.</span><span class="token function">drawImage</span><span class="token punctuation">(</span>img<span class="token punctuation">[</span><span class="token punctuation">,</span> sx<span class="token punctuation">,</span> sy<span class="token punctuation">,</span> swidth<span class="token punctuation">,</span> sheight<span class="token punctuation">]</span><span class="token punctuation">,</span> x<span class="token punctuation">,</span> y<span class="token punctuation">[</span><span class="token punctuation">,</span> width<span class="token punctuation">,</span> height<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><table><thead><tr><th>参数</th><th>描述</th></tr></thead><tbody><tr><td>img</td><td>可以是图片、视频、画布</td></tr><tr><td>sx</td><td>可选。开始剪切的 x 坐标位置。</td></tr><tr><td>sy</td><td>可选。开始剪切的 y 坐标位置。</td></tr><tr><td>swidth</td><td>可选。被剪切图像的宽度。</td></tr><tr><td>sheight</td><td>可选。被剪切图像的高度。</td></tr><tr><td>x</td><td>在画布上放置图像的 x 坐标位置。</td></tr><tr><td>y</td><td>在画布上放置图像的 y 坐标位置。</td></tr><tr><td>width</td><td>可选。要使用的图像的宽度。（伸展或缩小图像）</td></tr><tr><td>height</td><td>可选。要使用的图像的高度。（伸展或缩小图像）</td></tr></tbody></table><h3 id="getimagedata"><a href="#getimagedata" aria-hidden="true" class="header-anchor">#</a> getImageData</h3><p>用法：</p><pre class="language-js"><code>context<span class="token punctuation">.</span><span class="token function">getImageData</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> width<span class="token punctuation">,</span> height<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><table><thead><tr><th>参数</th><th>描述</th></tr></thead><tbody><tr><td>x</td><td>开始复制的左上角位置的 x 坐标。</td></tr><tr><td>y</td><td>开始复制的左上角位置的 y 坐标。</td></tr><tr><td>width</td><td>将要复制的矩形区域的宽度。</td></tr><tr><td>height</td><td>将要复制的矩形区域的高度。</td></tr></tbody></table><p>方法返回 ImageData 对象，该对象拷贝了画布指定矩形的像素数据。
是Uint8ClampedArray类型的一维数组，包含着RGBA格式的整型数据。
对于 ImageData 对象中的每个像素，都存在着四方面的信息，即RGBA值:</p><ul><li>R - 红色 (0-255)</li><li>G - 绿色 (0-255)</li><li>B - 蓝色 (0-255)</li><li>A - alpha 通道 (0-255; 0 是透明的，255 是完全可见的)</li></ul><p>color/alpha 以数组形式存在，并存储于 ImageData 对象的 data 属性中。</p><p>这个样子：
<img src="http://pn4meizzc.bkt.clouddn.com/canvas001.gif" alt></p><p>先来一下demo，通过getImageData方法获取鼠标指针处的像素值。</p><h3 id="demo1"><a href="#demo1" aria-hidden="true" class="header-anchor">#</a> demo1</h3><div class="pixelPick-wrapper" data-v-08c642a2><p id="color" data-v-08c642a2></p><canvas id="canvas" width="500" height="400" data-v-08c642a2></canvas></div><p>部分代码：</p><pre class="language-js"><code>methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
	<span class="token keyword">import</span> imgUrl <span class="token keyword">from</span> <span class="token string">'./component/sample.jpg'</span><span class="token punctuation">;</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
	<span class="token function">data</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token punctuation">{</span>
			canvas<span class="token punctuation">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span>
			ctx<span class="token punctuation">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span>
			color<span class="token punctuation">:</span> <span class="token keyword">null</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span><span class="token punctuation">,</span>

	methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
		<span class="token function">pick</span> <span class="token punctuation">(</span>e<span class="token punctuation">,</span> ctx<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token keyword">let</span> x <span class="token operator">=</span> e<span class="token punctuation">.</span>layerX<span class="token punctuation">,</span>
				y <span class="token operator">=</span> e<span class="token punctuation">.</span>layerY<span class="token punctuation">,</span>
				pixel <span class="token operator">=</span> ctx<span class="token punctuation">.</span><span class="token function">getImageData</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
				data <span class="token operator">=</span> pixel<span class="token punctuation">.</span>data<span class="token punctuation">,</span>
				rgba <span class="token operator">=</span> <span class="token string">'rgba('</span> <span class="token operator">+</span> data<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">','</span> <span class="token operator">+</span> data<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">','</span> <span class="token operator">+</span> data<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">','</span> <span class="token operator">+</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>data<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">/</span> <span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">')'</span><span class="token punctuation">;</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span>color<span class="token punctuation">.</span>style<span class="token punctuation">.</span>background <span class="token operator">=</span>  rgba<span class="token punctuation">;</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span>color<span class="token punctuation">.</span>textContent <span class="token operator">=</span> rgba<span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span><span class="token punctuation">,</span>

	<span class="token function">mounted</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>canvas <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">[</span><span class="token string">'canvas'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>ctx <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token string">'2d'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>color <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">[</span><span class="token string">'color'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

		<span class="token keyword">let</span> img <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Image</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		img<span class="token punctuation">.</span>src <span class="token operator">=</span> imgUrl<span class="token punctuation">;</span>

		img<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span>ctx<span class="token punctuation">.</span><span class="token function">drawImage</span><span class="token punctuation">(</span>img<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span>width<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span>height<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span><span class="token punctuation">;</span>
		
		<span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span><span class="token function-variable function">onmousemove</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">pick</span><span class="token punctuation">(</span>event<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>ctx<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><h2 id="前端图片预览、跨域图片问题"><a href="#前端图片预览、跨域图片问题" aria-hidden="true" class="header-anchor">#</a> 前端图片预览、跨域图片问题</h2><p>还可以取本地或者远程跨域的图片，像这样</p><h3 id="demo2"><a href="#demo2" aria-hidden="true" class="header-anchor">#</a> demo2</h3><div class="pixelPick-wrapper" data-v-6ce87936><input type="file" name data-v-6ce87936><div class="el-input el-input--small el-input-group el-input-group--append" data-v-6ce87936><!----><input type="text" autocomplete="off" placeholder="请输入图片url" value="http://pn4meizzc.bkt.clouddn.com/blog/else/miaoWechatIMG241526366731_.pic.jpg" class="el-input__inner"><!----><!----><div class="el-input-group__append"><button type="button" class="el-button el-button--default" data-v-6ce87936><!----><!----><span>确定</span></button></div></div><p id="color" data-v-6ce87936></p><canvas id="canvas" width="500" height="400" data-v-6ce87936></canvas></div><p>但这里有两个问题：一个是本地图片预览，一个是跨域图片报错。</p><p>第一个问题之前有写过一篇文章，可以看<a href="https://juejin.im/post/59f892286fb9a04511706f53" target="_blank" rel="noopener noreferrer">这里</a>，这里不赘述了。</p><div class="tip custom-block"><p class="custom-block-title">注意</p><p>第二个问题源于canvas无法对<strong>没有权限的跨域图片</strong>进行操作，如出现跨域，对图片的操作(如getImageData、canvas.toDataURL)会报错：<span class="txt-red">Uncaught DOMException: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.</span>
即canvas已经被跨域的数据污染了。</p><p>要解决这个问题，就需要图片所在的服务器允许跨域访问(设置消息头<span class="txt-green">Access-Control-Allow-Origin=&quot;*&quot;或者你的网站域名</span>)，且本地也需要开启跨域权限(<span class="txt-green">img.crossOrigin = &quot;anonymous&quot;</span>)。</p><p>由于一般的服务器都是允许跨域的，所以前端只要设置img.crossOrigin = &quot;anonymous&quot;就可以了。</p><p>当然，如果服务器设置了图片防盗链的话，我们本地开启了跨域权限也是没有用的。</p></div><p>部分代码：</p><pre class="language-js"><code><span class="token function">data</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">return</span> <span class="token punctuation">{</span>
		canvas<span class="token punctuation">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span>
		ctx<span class="token punctuation">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span>
		color<span class="token punctuation">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span>
		exterbalUrl<span class="token punctuation">:</span> <span class="token string">'http://pn4meizzc.bkt.clouddn.com/blog/else/miaoWechatIMG241526366731_.pic.jpg'</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
	<span class="token function">pick</span> <span class="token punctuation">(</span>e<span class="token punctuation">,</span> ctx<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">let</span> x <span class="token operator">=</span> e<span class="token punctuation">.</span>layerX<span class="token punctuation">,</span>
			y <span class="token operator">=</span> e<span class="token punctuation">.</span>layerY<span class="token punctuation">,</span>
			pixel <span class="token operator">=</span> ctx<span class="token punctuation">.</span><span class="token function">getImageData</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
			data <span class="token operator">=</span> pixel<span class="token punctuation">.</span>data<span class="token punctuation">,</span>
			rgba <span class="token operator">=</span> <span class="token string">'rgba('</span> <span class="token operator">+</span> data<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">','</span> <span class="token operator">+</span> data<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">','</span> <span class="token operator">+</span> data<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">','</span> <span class="token operator">+</span> <span class="token punctuation">(</span>data<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">/</span> <span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">')'</span><span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>color<span class="token punctuation">.</span>style<span class="token punctuation">.</span>background <span class="token operator">=</span>  rgba<span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>color<span class="token punctuation">.</span>textContent <span class="token operator">=</span> rgba<span class="token punctuation">;</span>
	<span class="token punctuation">}</span><span class="token punctuation">,</span>

	<span class="token function">onFileChange</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">let</span> file <span class="token operator">=</span> e<span class="token punctuation">.</span>target<span class="token punctuation">.</span>files<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
			blob <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Blob</span><span class="token punctuation">(</span><span class="token punctuation">[</span>file<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// 文件转化成二进制文件</span>
           	url <span class="token operator">=</span> <span class="token constant">URL</span><span class="token punctuation">.</span><span class="token function">createObjectURL</span><span class="token punctuation">(</span>blob<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//转化成url</span>
        <span class="token keyword">let</span> img <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Image</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		img<span class="token punctuation">.</span>src <span class="token operator">=</span> url<span class="token punctuation">;</span>
		img<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">draw</span><span class="token punctuation">(</span>img<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token constant">URL</span><span class="token punctuation">.</span><span class="token function">revokeObjectURL</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span><span class="token punctuation">,</span>

	<span class="token function">draw</span> <span class="token punctuation">(</span>img<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>ctx<span class="token punctuation">.</span><span class="token function">clearRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span>width<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span>height<span class="token punctuation">)</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>ctx<span class="token punctuation">.</span><span class="token function">drawImage</span><span class="token punctuation">(</span>img<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span>width<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span>height<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span><span class="token punctuation">,</span>

	<span class="token function">onConfirmUrl</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">let</span> img <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Image</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token comment">//解决跨域问题</span>
		img<span class="token punctuation">.</span>crossOrigin <span class="token operator">=</span> <span class="token string">'anonymous'</span><span class="token punctuation">;</span>
		img<span class="token punctuation">.</span>src <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>exterbalUrl<span class="token punctuation">;</span>
		img<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">draw</span><span class="token punctuation">(</span>img<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token function">mounted</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">this</span><span class="token punctuation">.</span>canvas <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">[</span><span class="token string">'canvas'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">this</span><span class="token punctuation">.</span>ctx <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token string">'2d'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">this</span><span class="token punctuation">.</span>color <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">[</span><span class="token string">'color'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	
	<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">onConfirmUrl</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span><span class="token function-variable function">onmousemove</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">pick</span><span class="token punctuation">(</span>event<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>ctx<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><h3 id="demo3"><a href="#demo3" aria-hidden="true" class="header-anchor">#</a> demo3</h3><p>下面是一个放大镜效果，类似PC端淘宝页面产品预览的效果。这样：</p><div class="pixelPick-wrapper" data-v-7a2f7199><input type="file" name data-v-7a2f7199><div class="el-input el-input--small el-input-group el-input-group--append" data-v-7a2f7199><!----><input type="text" autocomplete="off" placeholder="请输入图片url" value="http://pn4meizzc.bkt.clouddn.com/blog/else/miaoWechatIMG241526366731_.pic.jpg" class="el-input__inner"><!----><!----><div class="el-input-group__append"><button type="button" class="el-button el-button--default" data-v-7a2f7199><!----><!----><span>确定</span></button></div></div><div class="canvasWrapper" data-v-7a2f7199><canvas id="canvas" width="300" height="300" data-v-7a2f7199></canvas><canvas id="magnifier" width="308" height="308" data-v-7a2f7199></canvas></div></div><p>这个效果的实现相当简单，只是直接利用了drawImage的“截取”功能，把左侧截取的50 * 50的画布放大后，重新画在了新的画布上。</p><p>部分代码：</p><pre class="language-js"><code><span class="token keyword">const</span> <span class="token constant">SAMPLE_WIDTH</span> <span class="token operator">=</span> <span class="token number">50</span><span class="token punctuation">,</span>
	  <span class="token constant">CANVAS_WIDHT</span> <span class="token operator">=</span> <span class="token number">300</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
	<span class="token function">data</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token punctuation">{</span>
			exterbalUrl<span class="token punctuation">:</span> <span class="token string">'http://pn4meizzc.bkt.clouddn.com/blog/else/miaoWechatIMG241526366731_.pic.jpg'</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span><span class="token punctuation">,</span>

	methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
		<span class="token function">pick</span> <span class="token punctuation">(</span>e<span class="token punctuation">,</span> ctx<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token keyword">let</span> x <span class="token operator">=</span> e<span class="token punctuation">.</span>layerX<span class="token punctuation">,</span>
				y <span class="token operator">=</span> e<span class="token punctuation">.</span>layerY<span class="token punctuation">;</span>
				
			<span class="token keyword">if</span><span class="token punctuation">(</span>x <span class="token operator">&lt;</span> <span class="token constant">SAMPLE_WIDTH</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
				x <span class="token operator">=</span> <span class="token constant">SAMPLE_WIDTH</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>
			<span class="token punctuation">}</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>x <span class="token operator">&gt;</span> <span class="token constant">CANVAS_WIDHT</span> <span class="token operator">-</span> <span class="token constant">SAMPLE_WIDTH</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
				x <span class="token operator">=</span> <span class="token constant">CANVAS_WIDHT</span> <span class="token operator">-</span> <span class="token constant">SAMPLE_WIDTH</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>
			<span class="token punctuation">}</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>y <span class="token operator">&lt;</span> <span class="token constant">SAMPLE_WIDTH</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
				y <span class="token operator">=</span> <span class="token constant">SAMPLE_WIDTH</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>
			<span class="token punctuation">}</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>y <span class="token operator">&gt;</span> <span class="token constant">CANVAS_WIDHT</span> <span class="token operator">-</span> <span class="token constant">SAMPLE_WIDTH</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
				y <span class="token operator">=</span> <span class="token constant">CANVAS_WIDHT</span> <span class="token operator">-</span> <span class="token constant">SAMPLE_WIDTH</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>
			<span class="token punctuation">}</span>
			<span class="token keyword">let</span> x1 <span class="token operator">=</span> x <span class="token operator">-</span> <span class="token constant">SAMPLE_WIDTH</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">,</span>
				y1 <span class="token operator">=</span> y <span class="token operator">-</span> <span class="token constant">SAMPLE_WIDTH</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>

			<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">drawCanvas</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>img<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">showMagnifier</span><span class="token punctuation">(</span>x1<span class="token punctuation">,</span> y1<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">drawSampleFrame</span><span class="token punctuation">(</span>x1<span class="token punctuation">,</span> y1<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span><span class="token punctuation">,</span>

		<span class="token function">drawSampleFrame</span> <span class="token punctuation">(</span>x1<span class="token punctuation">,</span> y1<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span>ctx<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span>x1<span class="token punctuation">,</span> y1<span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span>ctx<span class="token punctuation">.</span><span class="token function">strokeRect</span><span class="token punctuation">(</span>x1<span class="token punctuation">,</span> y1<span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span><span class="token punctuation">,</span>

		<span class="token function">onFileChange</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token keyword">let</span> file <span class="token operator">=</span> e<span class="token punctuation">.</span>target<span class="token punctuation">.</span>files<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
				blob <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Blob</span><span class="token punctuation">(</span><span class="token punctuation">[</span>file<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// 文件转化成二进制文件</span>
               	url <span class="token operator">=</span> <span class="token constant">URL</span><span class="token punctuation">.</span><span class="token function">createObjectURL</span><span class="token punctuation">(</span>blob<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//转化成url</span>
            <span class="token keyword">let</span> img <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Image</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			img<span class="token punctuation">.</span>src <span class="token operator">=</span> url<span class="token punctuation">;</span>
			img<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
				<span class="token keyword">this</span><span class="token punctuation">.</span>img <span class="token operator">=</span> img<span class="token punctuation">;</span>
				<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">drawCanvas</span><span class="token punctuation">(</span>img<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token constant">URL</span><span class="token punctuation">.</span><span class="token function">revokeObjectURL</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">}</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span><span class="token punctuation">,</span>

		<span class="token function">drawCanvas</span> <span class="token punctuation">(</span>img<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span>ctx<span class="token punctuation">.</span><span class="token function">clearRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span>width<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span>height<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span>ctx<span class="token punctuation">.</span><span class="token function">drawImage</span><span class="token punctuation">(</span>img<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span>width<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span>height<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span><span class="token punctuation">,</span>

		<span class="token function">onConfirmUrl</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token keyword">let</span> img <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Image</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token comment">//解决跨域问题</span>
			img<span class="token punctuation">.</span>crossOrigin <span class="token operator">=</span> <span class="token string">'anonymous'</span><span class="token punctuation">;</span>
			img<span class="token punctuation">.</span>src <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>exterbalUrl<span class="token punctuation">;</span>
			img<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
				<span class="token keyword">this</span><span class="token punctuation">.</span>img <span class="token operator">=</span> img<span class="token punctuation">;</span>
				<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">drawCanvas</span><span class="token punctuation">(</span>img<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">}</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span><span class="token punctuation">,</span>

		<span class="token function">showMagnifier</span> <span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token comment">//重点所在</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span>magnifierCtx<span class="token punctuation">.</span><span class="token function">drawImage</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">,</span> x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> <span class="token constant">SAMPLE_WIDTH</span><span class="token punctuation">,</span> <span class="token constant">SAMPLE_WIDTH</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>magnifier<span class="token punctuation">.</span>width<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>magnifier<span class="token punctuation">.</span>height<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span><span class="token punctuation">,</span>

	<span class="token function">mounted</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>canvas <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">[</span><span class="token string">'canvas'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>magnifier <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">[</span><span class="token string">'magnifier'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>ctx <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token string">'2d'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>magnifierCtx <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>magnifier<span class="token punctuation">.</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token string">'2d'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

		<span class="token keyword">this</span><span class="token punctuation">.</span>ctx<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">'rgba(30, 144, 255, .5)'</span><span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>ctx<span class="token punctuation">.</span>strokeStyle <span class="token operator">=</span> <span class="token string">'#000'</span><span class="token punctuation">;</span>

		<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">onConfirmUrl</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		
		<span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span><span class="token function-variable function">onmousemove</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">pick</span><span class="token punctuation">(</span>event<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>ctx<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>

		<span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span><span class="token function-variable function">onmouseout</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span>magnifierCtx<span class="token punctuation">.</span><span class="token function">clearRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>magnifier<span class="token punctuation">.</span>width<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>magnifier<span class="token punctuation">.</span>height<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">drawCanvas</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>img<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><p>另一篇：<a href="https://denzel.netlify.com/html/canvas_video_green_screen_cutout.html" target="_blank" rel="noopener noreferrer">canvas像素点操作 —— 视频绿幕抠图</a></p><h2 id="参考资料"><a href="#参考资料" aria-hidden="true" class="header-anchor">#</a> 参考资料</h2><ul><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Pixel_manipulation_with_canvas" target="_blank" rel="noopener noreferrer">Pixel manipulation with canvas</a></li><li><a href="https://codepo8.github.io/canvas-images-and-pixels/" target="_blank" rel="noopener noreferrer">Canvas and images and pixels</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-crossorigin#Attributes" target="_blank" rel="noopener noreferrer">The Image Embed element</a></li></ul></div><div class="wx-wrapper" data-v-0de8a4bf><p class="wx-txt" data-v-0de8a4bf>欢迎关注</p><img src="http://pn4meizzc.bkt.clouddn.com/wxqr.png" class="wx-img" data-v-0de8a4bf></div><div class="beg-wrapper" data-v-1f1af99c><a href="javascript:;" class="btn show" data-v-1f1af99c><img src="http://pn4meizzc.bkt.clouddn.com/blogshang_log.png" data-v-1f1af99c></a><p class="shang-txt" data-v-1f1af99c>赏不赏，看您心情</p><img src="http://pn4meizzc.bkt.clouddn.com/blogWechatIMG35.jpeg" class="shangImg" data-v-1f1af99c></div><!----><div class="content page-nav"><p class="inner"><span class="prev">
        ← <a href="/blog/vuepress/html/canvas_video_green_screen_cutout.html" class="prev">
          canvas像素点操作 —— 视频绿幕抠图
        </a></span><span class="next"><a href="/blog/vuepress/html/dialog_element.html">
          HTML5.2新标签 —— dialog 
        </a> →
      </span></p></div></div></div></div>
    <script src="/blog/vuepress/assets/js/43.8f0b2d6c.js" defer></script><script src="/blog/vuepress/assets/js/85.44e980f9.js" defer></script><script src="/blog/vuepress/assets/js/84.1ffe6969.js" defer></script><script src="/blog/vuepress/assets/js/87.cd49f324.js" defer></script><script src="/blog/vuepress/assets/js/app.15770e34.js" defer></script>
  </body>
</html>
